const Service = require("egg").Service;

class AdminOrder extends Service {
  // 修改购物车中商品的数量
  async updateAdminOrder() {
    let id = this.ctx.params.id;

    let state = await this.app.model.Order.findAll({
      where: {
        id
      }
    });

    //当值为1时表示用户已完成付款 可以修改订单物流单号
    if (state[0].dataValues.order_state == 1) {
      let logistics = this.ctx.request.body.logistics;

      let res = await this.app.model.Order.update(
        { logistics },
        {
          where: {
            id
          }
        }
      );

      if (!res) return { msg: "添加失败" };

      return {
        msg: "添加成功"
      };
    }

    return {
      msg: "只有订单状态在1的时候 才可以添加物流单号"
    };
  }

  // 根据用户id查询购物车列表
  async adminQueryAllOrder() {
    try {
      let resShop = await this.app.model.Order.findAll({
        include: [
          {
            model: this.app.model.UserInfo,
            as: "userInfo"
          },
          {
            model: this.app.model.Commodity,
            as: "shop"
          },
          {
            model: this.app.model.User,
            as: "user"
          }
        ]
      });
      if (!resShop) return { msg: "查询失败" };

      return resShop;
    } catch (e) {
      console.log(e);
    }
  }

  // 根据订单编号查询订单
  async queryOrderByNo() {
    let { order_no } = this.ctx.query;

    let state = await this.app.model.Order.findAll({
      where: {
        order_no
      },
      include: [
        {
          model: this.app.model.UserInfo,
          as: "userInfo"
        },
        {
          model: this.app.model.Commodity,
          as: "shop"
        },
        {
          model: this.app.model.User,
          as: "user"
        }
      ]
    });

    if (!state) return { msg: "查询失败" };

    return state;
  }
}
module.exports = AdminOrder;
